/*
 * FileName: CmqConsumeMessageHook2Impl.java
 * Author:   zzw
 * Date:     2019年01月15日
 * Description:
 *//*

package com.zzw.rocketmq;

import com.chezhibao.cmq.client.trace.common.CMQType;
import com.chezhibao.cmq.client.trace.common.TraceBean;
import com.chezhibao.cmq.client.trace.common.TraceContext;
import com.chezhibao.cmq.client.trace.hook.CmqConsumeMessageHookImpl;
import com.chezhibao.cmq.client.trace.sub.CmqConsumeMessageTraceLog;
import com.chezhibao.cmq.client.trace.utils.Utils;
import org.apache.rocketmq.client.hook.ConsumeMessageContext;
import org.apache.rocketmq.common.message.MessageAccessor;
import org.apache.rocketmq.common.message.MessageClientExt;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

*/
/**
 * 〈〉<br>
 * 〈〉
 *
 * @author zzw
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本]（可选）
 *//*

public class CmqConsumeMessageHook2Impl extends CmqConsumeMessageHookImpl {

    @Override
    public void consumeMessageBefore(ConsumeMessageContext context) {
        if (context != null && context.getMsgList() != null && !context.getMsgList().isEmpty()) {
            TraceContext mqTraceContext = new TraceContext();
            context.setMqTraceContext(mqTraceContext);
            mqTraceContext.setMqType(CMQType.ROCKETMQ);
            mqTraceContext.setGroup(context.getConsumerGroup());
            List<TraceBean> beans = new ArrayList();
            Iterator i$ = context.getMsgList().iterator();

            while(i$.hasNext()) {
                MessageExt message = (MessageExt)i$.next();
                if (message != null) {
                    MessageClientExt msg = (MessageClientExt)message;
                    TraceBean traceBean = new TraceBean();

                    traceBean.setTraceId(msg.getProperty("clothoTraceId"));
                    if(message.getReconsumeTimes() > 0){
                        StringBuffer stringBuffer = new StringBuffer();
                        traceBean.setRpcId(stringBuffer.append(msg.getProperty("clothoRpcId")).append("_").append("" + message.getReconsumeTimes()).toString());
                    }
                    else {
                        traceBean.setRpcId(msg.getProperty("clothoRpcId"));
                    }

                    System.out.println("before rpcId:" + msg.getProperty("clothoRpcId"));
                    traceBean.setClothoUserData(msg.getProperty("clothoUserData"));
                    traceBean.setTopic(msg.getTopic());
                    traceBean.setMsgId(msg.getMsgId());
                    traceBean.setOriginMsgId(msg.getMsgId());
                    traceBean.setTags(msg.getTags());
                    traceBean.setKeys(msg.getKeys());
                    traceBean.setBuyerId(msg.getBuyerId());
                    traceBean.setTransferFlag(MessageAccessor.getTransferFlag(msg));
                    traceBean.setCorrectionFlag(MessageAccessor.getCorrectionFlag(msg));
                    traceBean.setBodyLength(msg.getBody().length);
                    traceBean.setBornHost(Utils.substring(msg.getBornHost().toString(), 1));
                    traceBean.setStoreHost(Utils.substring(msg.getStoreHost().toString(), 1));
                    traceBean.setStoreTime(msg.getStoreTimestamp());
                    traceBean.setBrokerName(context.getMq().getBrokerName());
                    traceBean.setQueueId(msg.getQueueId());
                    traceBean.setOffset(msg.getQueueOffset());
                    traceBean.setRetryTimes(msg.getReconsumeTimes());
                    traceBean.setProps(context.getProps());
                    String regionId = msg.getProperty("MSG_REGION");
                    if (regionId == null) {
                        regionId = "DEFAULT_REGION";
                    }

                    mqTraceContext.setRegionId(regionId);
                    beans.add(traceBean);
                }
            }

            mqTraceContext.setTraceBeans(beans);
            CmqConsumeMessageTraceLog.consumeMessageBefore(mqTraceContext);
        }
    }

    //解决mq重试rpcid不变造成链路错乱
    */
/*@Override
    public void consumeMessageAfter(ConsumeMessageContext context) {
        List<MessageExt> msgList = context.getMsgList();
        if (context != null && msgList != null && !msgList.isEmpty()) {
            TraceContext traceContext = (TraceContext)context.getMqTraceContext();
            traceContext.setSuccess(context.isSuccess());
            traceContext.setStatus(context.getStatus());
            CmqConsumeMessageTraceLog.consumeMessageAfter(traceContext);
            List<TraceBean> traceBeans = traceContext.getTraceBeans();

            if(context.getStatus().equals(ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT.name())){
                for(MessageExt ext:msgList){
                    Map<String, String> properties = ext.getProperties();
                    String[] split = properties.get("clothoRpcId").split("\\.");
                    int i = Integer.parseInt(split[split.length - 1]) + 1;
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int j = 0;j < split.length - 1;j++){
                        stringBuilder.append(split[j]);
                    }
                    stringBuilder.append(".").append(i);
                    properties.put("clothoRpcId",stringBuilder.toString());
                    System.out.println("after rpcId:" + stringBuilder.toString());
                }
            }
        }
    }*//*

}
*/
